﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlaneKeyboardController : MonoBehaviour
{
    public KeyCode up;
    public KeyCode down;
    public KeyCode left;
    public KeyCode right;
    public KeyCode fire;
    private Plane _cachedPlane;
    // Start is called before the first frame update
    void Start()
    {
        _CachePlane();
    }

    void _CachePlane() {
        if(!_cachedPlane) {
            _cachedPlane = GetComponent<Plane>();
        }
    }

    // Update is called once per frame
    void Update()
    {
        if(_cachedPlane) 
        {
            if(Input.GetKey(up)) {
                this._cachedPlane.setMotor(1, 1);
            } else if(Input.GetKey(down)) {
                this._cachedPlane.setMotor(1, -1);
            } else {
                this._cachedPlane.setMotor(1, 0);
            }
            if(Input.GetKey(right)) {
                this._cachedPlane.setMotor(0, 1);
            } else if(Input.GetKey(left)) {
                this._cachedPlane.setMotor(0, -1);
            } else {
                this._cachedPlane.setMotor(0, 0);
            }
            if(Input.GetKey(fire))
            {
                this._cachedPlane.Fire();
            }
        }
    }
}
